//
//  WCExtractHtml.m
//  WebClipper
//
//  Created by Daniel Braun on 04/04/07.
//  Copyright 2007 Daniel Braun http://page-appart.net, http://braun.daniel.free.fr All rights reserved.
//

#import "WCExtractHtml.h"
#import "DomExtension.h"

@implementation WCExtractHtml

+ (NSString *) description
{
	return @"keep  html";
}

- (void) dealloc {
	[cachedContent release];
	[super dealloc];
}

- (id) init {
	self = [super init];
	if (self != nil) {
		keepNext=1;
	}
	return self;
}

- (id) initWithCoder:(NSCoder *) coder
{
	self=[super initWithCoder:coder];
	if ([coder allowsKeyedCoding]) {
		keepNext=[coder decodeIntForKey:@"keepNext"];
	}
	return self;
}

- (void)encodeWithCoder:(NSCoder *)coder
{
	[super encodeWithCoder:coder];
	if ( [coder allowsKeyedCoding] ) {
		[coder encodeInt:keepNext forKey:@"keepNext"];
	}
}

- (NSNumber *) keepNext
{
	return [NSNumber numberWithInt:keepNext];
}
- (void) setKeepNext:(NSNumber *)n
{
	keepNext=[n intValue];
}

- (id) handle:(DOMHTMLElement *)el fromUrl:(NSURL *)url
{
	int i;
	[cachedContent release];
	// this is really what WCExtractAction is doing
	//NSLog(@"keep %d items\n", keepNext);
	cachedContent=nil;
	for (i=0;i<keepNext; i++) {
		NSString *s=[super handle:el fromUrl:url];
		if (!s) break;
		if (!cachedContent) cachedContent=s;
		else cachedContent=[cachedContent stringByAppendingString:s];
		el=(DOMHTMLElement *)[el nextElement];
	}
	[cachedContent retain];
	return cachedContent;
}

- (NSString *) htmlContent
{
	return cachedContent;
}


@end
